# functions and constants used across multiple scripts

stars <- c(0.001, 0.01, 0.05, 0.1)
coef_map <- list("resource_and_agriculture" = "Natural resources and agriculture",
                 "resource_and_agriculture_15" = "Natural resources and agriculture",
                 "resource_and_agriculture_20" = "Natural resources and agriculture",
                 "none_one_or_both_nats_agri_charNone" = "Neither resources nor agri (baseline: both)",
                 "none_one_or_both_nats_agri_charSingle" = "Resources or agri (baseline: both)",
                 "none_one_or_both_nats_agri_char_15None" = "Neither resources nor agri (baseline: single)",
                 "none_one_or_both_nats_agri_char_15Single" = "Resources or agri (baseline: single)",
                 "none_one_or_both_nats_agri_char_15Both" = "Resources and agri (baseline: single)",
                 "none_one_or_both_nats_agri_char_20None" = "Neither resources nor agri (baseline: single)",
                 "none_one_or_both_nats_agri_char_20Single" = "Resources or agri (baseline: single)",
                 "none_one_or_both_nats_agri_char_20Both" = "Resources and agri (baseline: single)",
                 "both_or_whichNatural Resources" = "Rents: Natural resources (baseline: none)",
                 "both_or_whichAgricultural Production" = "Rents: Agricultural Production",
                 "both_or_whichBoth" = "Rents: Both income sources",
                 "religious_segments_bin" = "More than one religious segment",
                 "alignment_and_multiple_segments" = "Religious alignment and multiple segments",
                 "religious_constellationunity" = "Constellation: Unity (baseline: aligned frac.)",
                 "religious_constellationunaligned_frac" = "Constellation: Unaligned Fractionalization",
                 "religious_favoritism_and_multiple_segments" = "Group religion favored and fractionalization",
                 "favoritism_constellationunity" = "Constellation: Unity (baseline: favoristim and fractionalization)",
                 "favoritism_constellationno_favoritism_frac" = "Constellation: Not favored and fractionalization",
                 "linguistic_segments_bin" = "More than one linguistic segment",
                 "n_ed_langs" = "Number of linguistic segments",
                 "hhi_lang" = "Linguistic fractionalization (HHI)",
                 "resource_and_pasture" = "Natural resources and pasture",
                 "none_one_or_both_nats_cattle_charSingle" = "Resources or pasture (baseline: both)",
                 "none_one_or_both_nats_cattle_charNone" = "Neither resources nor pasture (baseline: both)",
                 "n_ed_religions" = "Number of religious segments",
                 "hhi_rel" = "Religious fractionalization (HHI)",
                 "n_non_intersection_group_polygons" = "No. settlement areas",
                 "multiple_polygons_bin" = "Several settlement areas (0/1)",
                 "spatial_hhi" = "Geographic fractionalization (HHI)",
                 "n_orgs" = "Number of organizations",
                 "regaut" = "Regional autonomy (EPR)",
                 "groupsize" = "Group share",
                 "incidence_flag" = "Violent conflict",
                 "status_pwrrank" = "Power status (EPR)",
                 "nightlight_total_pc_log" = "Group nightlights (log)",
                 "n_tek_groups" = "N kin groups",
                 "any_multiethnic" = "Any multiethnic organizations",
                 "countries_gwid" = "Country")



# Mode function
# taken from here: https://stackoverflow.com/questions/2547402/how-to-find-the-statistical-mode
# otherwise won't work for character vectors
Mode <- function(x, na.rm = FALSE) {
  if(na.rm){
    x = x[!is.na(x)]
  }
  
  ux <- unique(x)
  return(ux[which.max(tabulate(match(x, ux)))])
}
